package com.listener.interceptor;

import com.listener.dao.MoveDao;
import com.listener.dao.TypeDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.atomic.AtomicLong;

public class CoreInterceptor implements HandlerInterceptor {
    @Autowired
    TypeDao typeDao;
    @Autowired
    MoveDao moveDao;
    private AtomicLong counter = new AtomicLong(0);
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 在这里添加属性到请求中
        if(request.getSession().getAttribute("typeColors")==null){
            request.getSession().setAttribute("typeColors",
                    typeDao.selectAllTypeColor());
        }
        if(request.getSession().getAttribute("types")==null){
            request.getSession().setAttribute("types",
                    typeDao.selectAllType());
        }
        if(request.getSession().getAttribute("categories")==null){
            request.getSession().setAttribute("categories",
                    moveDao.selectAllCategory());
        }
        request.setAttribute("counter", counter.incrementAndGet());
        return true; // 返回true表示继续处理请求，返回false则中断请求处理
    }
}
